/*
#########################################################
#														#
#		Slawomir Mitak 									#
#		email: slawomirmitak@yahoo.com					#
#		form to search thru DB with suggestions			#
#														#
#########################################################*/

var kws;

function KeyPress(e)
{
    var iKeyCode;
    if (!e) {
    var e = window.event;
    }

    if (e.keyCode) {
        iKeyCode = e.keyCode;
        } else {
        if (e.which) {
          iKeyCode = e.which;
        }
    }
	switch (iKeyCode){//switch
		case 40:
			//=========================================DOWN===================================================
			if(iKeyCode == 40 && countDiv()>0){
				//check witch div is highlited
				var i=0;
				var hDiv=0;
				var cDiv=countDiv();
				for(i=1;i<=cDiv;i++){
					if(document.getElementById('prop'+i).className=='proposalHighlight'){
						if(hDiv<cDiv){document.getElementById('prop'+i).className ='proposal';}
						hDiv=i;
						}
					}
				//end highlited
				if(hDiv>0 && hDiv<cDiv){
					document.getElementById('prop'+(hDiv+1)).className='proposalHighlight';
					
					document.getElementById('searchKeyword').value=prepText(document.getElementById('prop'+(hDiv+1)).innerHTML);
					}else{
						document.getElementById('prop1').className='proposalHighlight';
						document.getElementById('searchKeyword').value=prepText(document.getElementById('prop1').innerHTML);
					}
				}
			//=========================================END DOWN=================================================
		break;
		case 38:
			//=========================================UP=======================================================
			if(iKeyCode == 38 && countDiv()>0){
			//check witch div is highlited
			var i=0;
			var hDiv=0;
			var cDiv=countDiv();
			for(i=1;i<=cDiv;i++){			
				if(document.getElementById('prop'+i).className=='proposalHighlight'){
					if(hDiv<cDiv){document.getElementById('prop'+i).className ='proposal';}
					hDiv=i;
					}				
				}
			//end highlited	
			if(hDiv>1 && hDiv<=cDiv){document.getElementById('prop'+(hDiv-1)).className='proposalHighlight';
			document.getElementById('searchKeyword').value=prepText(document.getElementById('prop'+(hDiv-1)).innerHTML);
			}else{
				document.getElementById('searchKeyword').value=prepText(document.getElementById('remKw').value);}
			}
			//=========================================END UP====================================================
		break;
		default:
			//remember start value	
			if(iKeyCode!=40 && iKeyCode!=38){
				document.getElementById('remKw').value=document.getElementById('searchKeyword').value; 
				kws=document.getElementById('searchKeyword').value; 
				
				if(document.getElementById('searchKeyword').value.length>0){MakeRequest();}else{
					document.getElementById('searchTerms').style.display='none';
					}
			}
			//remember start value end		
		}//end switch	
}
//prepage text
function prepText(text){
	text = text.replace(/<b>/g,'');
	text = text.replace(/<\/b>/g,'');
	return text;
	}
//count suggestions divs on page
function countDiv() {
var aDiv = document.getElementsByTagName('div');
var el, i = 0, howmany = 0;
while (el = aDiv.item(i++)) if (el.id.substr(0,4) == 'prop') howmany++;
return howmany;
}
//count suggestions divs on page
//submit value
function rStr (id){
	document.getElementById('searchKeyword').value = prepText(document.getElementById(id).innerHTML);
	document.getElementById('searchTerms').style.display = 'none';
	document.searchForm.submit();
	}
//submit value
//highlith DIV
function hightDiv(id){
	var i=0;
	var cDiv=countDiv();
	for(i=1;i<=cDiv;i++){document.getElementById('prop'+i).className='proposal';}
	document.getElementById(id).className='proposalHighlight';
	//send value to search term input
	//document.getElementById('searchKeyword').value = document.getElementById(id).innerHTML;
	}
//hightlDiv

//=======================================Request==========================================
function getXMLHttp(){
  var xmlHttp
  try
  {//Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {//Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}

function MakeRequest(){
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
			HandleResponse(xmlHttp.responseText);
			}
  }
  xmlHttp.open("GET", "showhelp.php?keword="+kws, true); 
  xmlHttp.send(null);
}

function HandleResponse(response){
	if(response.length>0){
		document.getElementById('searchTerms').style.display='block';
		document.getElementById('searchTerms').innerHTML=response;}
	else{
		document.getElementById('searchTerms').style.display='none';
		}
}
